package com.zh.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zh.dto.DishDto;
import com.zh.entity.Dish;
import com.zh.entity.DishFlavor;
import com.zh.mapper.DishMapper;
import com.zh.service.DishFlavorsService;
import com.zh.service.DishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜品管理 服务实现类
 * </p>
 *
 * @author 你爹
 * @since 2023-09-21
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorsService dishFlavorsService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    @Override
    @Transactional
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public void saveWithFlavors(DishDto dishDto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto, dish);
        //保存dish
        this.save(dish);
        //保存dish flavors
        Long dishId = dish.getId();
        if (dishDto.getFlavors() != null && dishDto.getFlavors().size() > 0) {
            for (DishFlavor flavor : dishDto.getFlavors()) {
                flavor.setDishId(dishId);
            }
            log.info("设置dishId是否成功?:{}", dishDto.getFlavors().get(0).getDishId());
        }
        dishFlavorsService.saveBatch(dishDto.getFlavors());
    }

    @Override
    public DishDto getDishById(Long id) {
        //dish基本信息
        Dish dish = this.getById(id);
        log.info("查询到的dish;{}", dish);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        //查口味
        LambdaUpdateWrapper<DishFlavor> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavors = dishFlavorsService.list(wrapper);
        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }
    @Transactional
    @Override
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public void updateWithFlavors(DishDto dishDto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto, dish);
        //更新dish
        this.updateById(dish);
        //更新dish flavors
        Long dishId = dish.getId();
        LambdaUpdateWrapper<DishFlavor> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dishId);
        dishFlavorsService.remove(wrapper);
        if (dishDto.getFlavors() != null && dishDto.getFlavors().size() > 0) {
           List<DishFlavor>  flavors = dishDto.getFlavors().stream().peek(flavor ->
                   flavor.setDishId(dishId)).collect(Collectors.toList());
           dishFlavorsService.saveBatch(flavors);
        }
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public void updateStatus(Integer status, List<Long> ids) {
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            this.updateById(dish);
        }
    }
    @Transactional
    @Override
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public void deleteWithFlavors(List<Long> ids) {
        ids.forEach(dishId->{
            //先删口味数据
            LambdaUpdateWrapper<DishFlavor> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(DishFlavor::getDishId,dishId);
            dishFlavorsService.remove(wrapper);
            //再删本条
            this.removeById(dishId);
        });
    }

    @Override
    @Cacheable(cacheNames = "dish", key = "'dish_'+#dish.categoryId",sync = true)
    public List<DishDto> listWithFlavors(Dish dish) {

        LambdaUpdateWrapper<Dish> wrapper =new LambdaUpdateWrapper<>();
        wrapper.eq(dish.getStatus()!=null,Dish::getStatus,dish.getStatus());
        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        wrapper.like(dish.getName()!=null,Dish::getName,dish.getName());
        wrapper.orderByDesc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishList = this.list(wrapper);
        List<DishDto> dishDtoList = new ArrayList<>();
        log.info("dishList:{}",dishList);
        dishList.stream().peek(d -> {
            LambdaUpdateWrapper<DishFlavor> wrapper1 = new LambdaUpdateWrapper<>();
            wrapper1.eq(DishFlavor::getDishId, d.getId());
            List<DishFlavor> dishFlavorList = dishFlavorsService.list(wrapper1);
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(d, dishDto);
            dishDto.setFlavors(dishFlavorList);
            dishDtoList.add(dishDto);
        }).collect(Collectors.toList());
        log.info("Impl: dishDtoList:{}",dishDtoList);
        return dishDtoList;
    }

}